package net.m2technologies.open_arm.transaction;

import org.opengroup.arm40.transaction.ArmApplication;
import org.opengroup.arm40.transaction.ArmApplicationDefinition;
import org.opengroup.arm40.transaction.ArmIdentityProperties;
import org.opengroup.arm40.transaction.ArmTransactionFactory;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
public class ArmApplicationImpl extends ArmInterfaceImpl implements ArmApplication {

    private final ArmApplicationDefinition appDef;
    private final ArmIdentityProperties contextProperties;
    private final String group;
    private final String instance;

    protected ArmApplicationImpl(final ArmApplicationDefinition appDef,
                                 final String[] contextValues,
                                 final String group,
                                 final String instance,
                                 final ArmTransactionFactory creatingFactory) {
        super(creatingFactory);
        this.appDef = appDef;
        final ArmIdentityPropertiesImpl existingProps = (ArmIdentityPropertiesImpl) appDef.getIdentityProperties();
        if (null != existingProps) {
            this.contextProperties
                    = new ArmIdentityPropertiesImpl((ArmIdentityPropertiesImpl) appDef.getIdentityProperties());
            ((ArmIdentityPropertiesTransactionImpl) this.contextProperties).setContextValues(contextValues);
        } else {
            this.contextProperties = null;
        }
        this.group = group;
        this.instance = instance;
    }

    public int end() {
        return 0;
    }

    public String getContextValue(final int index) {
        return ((ArmIdentityPropertiesImpl) this.contextProperties).getContextValue(index);
    }

    public ArmApplicationDefinition getDefinition() {
        return this.appDef;
    }

    public String getGroup() {
        return this.group;
    }

    public String getInstance() {
        return this.instance;
    }

    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof ArmApplicationImpl)) return false;

        final ArmApplicationImpl armApplication = (ArmApplicationImpl) o;

        return null != appDef
               ? !appDef.equals(armApplication.appDef)
               : null == armApplication.appDef && null != contextProperties
                 ? !contextProperties.equals(armApplication.contextProperties)
                 : null == armApplication.contextProperties && null != group
                   ? !group.equals(armApplication.group)
                   : null == armApplication.group && !(null != instance
                                                       ? !instance.equals(armApplication.instance)
                                                       : null != armApplication.instance);

    }

    public int hashCode() {
        int result;
        result = null != appDef ? appDef.hashCode() : 0;
        result = HASH_CODE_MAGIC_NUMBER * result + (null != contextProperties ? contextProperties.hashCode() : 0);
        result = HASH_CODE_MAGIC_NUMBER * result + (null != group ? group.hashCode() : 0);
        result = HASH_CODE_MAGIC_NUMBER * result + (null != instance ? instance.hashCode() : 0);
        return result;
    }

}
